package com.example.lancooznbk.view.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import com.andview.refreshview.XRefreshView;

/**
 */

public class DecoratorViewPager extends ViewPager {
    float mDownX;
    float mDownY;
    private boolean first = true;
    private boolean drop_down = false;
    private boolean on_pull = false;
    public XRefreshView parent;
    public DecoratorViewPager(Context context) {
        super(context);
    }

    public DecoratorViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public void setNestedpParent(XRefreshView parent) {
              this.parent = parent;
    }


    /*public boolean onInterceptTouchEvent(MotionEvent ev) {
        //在拦截处处理，在滑动设置了点击事件的地方也能swip，点击时又不能影响原来的点击事件
        int action = ev.getAction();
//        parent.requestDisallowInterceptTouchEvent(false);
        Log.v("onInterceptTouchEvent","ACTION_MOVE1"+super.onInterceptTouchEvent(ev));
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDownY = ev.getY();
                Log.v("aaaaaa ACTION_DOWN","ACTION_DOWN");
//                parent.setMoveForHorizontal(true);
                return true;
            case MotionEvent.ACTION_MOVE:
                float dy = Math.abs((ev.getY() - mDownY));
                float dx = Math.abs((ev.getX() - mDownX));
                Log.v("aaaaaa ACTION_MOVE","ACTION_MOVE1");
                if ((Math.abs(dy) < Math.abs(dx) && first == true) || drop_down ==true) {
                    //每次拦截的down都把触摸状态设置成了TOUCH_STATE_NONE 只有返回true才会走onTouchEvent 所以写在这里就够了
                    first = false;
                    drop_down = true;
                    Log.v("aaaaaa ACTION_MOVE","ACTION_MOVE2");
//                    parent.setMoveForHorizontal(true);
                    parent.requestDisallowInterceptTouchEvent(true);
                    return true;
                } else if((Math.abs(dy) > Math.abs(dx) && first == true) || on_pull ==true){
                    on_pull = true;
                    first = false;
                    Log.v("aaaaaa ACTION_MOVE","ACTION_MOVE3");
//                    parent.setMoveForHorizontal(false);
                    return true;
                }
                return false;
            case MotionEvent.ACTION_UP:
                first = true;
                drop_down = false;
                on_pull = false;
                Log.v("aaaaaa ACTION_UP","ACTION_UP");
//                parent.setMoveForHorizontal(true);
                return false;
            case MotionEvent.ACTION_CANCEL:
                first = true;
                drop_down = false;
                on_pull = false;
                Log.v("aaaaaa ACTION_CANCEL","ACTION_CANCEL");
//                parent.setMoveForHorizontal(true);
                return false;
        }
        Log.v("aaaaaa ACTION_CANCEL","ACTION_CANCEL"+super.onInterceptTouchEvent(ev));
        return super.onInterceptTouchEvent(ev);
    }*/
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (parent != null) {
            Log.i("dispatch_touch_event","---"+ev.getAction());
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (parent != null) {
            Log.i("dispatch_touch_event","---"+arg0.getAction());
        }
        return super.onInterceptTouchEvent(arg0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(true);
        }
        return super.onTouchEvent(arg0);
    }
}
